package com.vtb.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vtb.base.VtbBaseServlet;
import com.vtb.entity.AllInfo;
import com.vtb.entity.DataCache;
import com.vtb.entity.QqDetailInfo;
import com.vtb.entity.QqOauth;
import com.vtb.entity.RrDetailInfo;
import com.vtb.entity.RrOauth;
import com.vtb.utils.JdbcUtils;
@SuppressWarnings("serial")
public class AdminServlet extends VtbBaseServlet {
	private static final Log log = LogFactory.getLog(AdminServlet.class);
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		super.doPost(request, response);
		String method = request.getParameter("method");
		if ("del".equals(method)) {
			String uni = request.getParameter("uni");
			if (delInfo(uni)) {
				response.sendRedirect("tacheno");
			}
		}else {
			Map<String, QqDetailInfo> qqdatailMap = new HashMap<String, QqDetailInfo>();
			List<AllInfo> allInfoList = new ArrayList<AllInfo>();
			try {
				List<QqDetailInfo> qq_all = qqDao.getAllInfo();
				List<RrDetailInfo> rr_all = rrDao.getAllInfo();
				for (QqDetailInfo qqDetailInfo : qq_all) {
					qqdatailMap.put(qqDetailInfo.getRefr(), qqDetailInfo);
				}
				for (RrDetailInfo rrDetailInfo : rr_all) {
					AllInfo allInfo = new AllInfo();
					allInfo.setUni(rrDetailInfo.getUni());
					allInfo.setCreateTime(rrDetailInfo.getCreateTime());
					allInfo.setExpires_in(rrDetailInfo.getExpires_in());
					allInfo.setR2q(rrDetailInfo.getR2q());
					allInfo.setName(rrDetailInfo.getName());
					allInfo.setSex(rrDetailInfo.getSex());
					allInfo.setHometown(rrDetailInfo.getHometown());
					allInfo.setUniversity(rrDetailInfo.getUniversity());
					allInfo.setEmail(qqdatailMap.get(rrDetailInfo.getUni()).getEmail());
					allInfo.setNick(qqdatailMap.get(rrDetailInfo.getUni()).getNick());
					allInfo.setQq_name(qqdatailMap.get(rrDetailInfo.getUni()).getUni());
					allInfo.setLocation(qqdatailMap.get(rrDetailInfo.getUni()).getLocation());
					allInfo.setQ2r(qqdatailMap.get(rrDetailInfo.getUni()).getQ2r());
					allInfoList.add(allInfo);
				}
				request.setAttribute("allInfo", allInfoList);
				request.getRequestDispatcher("admin.jsp").forward(request, response);
			} catch (Exception e) {
				log.error("读取后台信息失败", e);
			}finally{
				JdbcUtils.close();
			}
		}
	}
	
	public boolean delInfo(String uni){
		RrOauth rrOauth = DataCache.rrmap.get(uni);
		QqOauth qqOauth = DataCache.qqmap.get(rrOauth.getRefq());
		try {
			qqDao.del(qqOauth);
			rrDao.del(rrOauth);
			
			JdbcUtils.commit();
			return true;
		} catch (Exception e) {
			JdbcUtils.rollback();
		}finally{
			JdbcUtils.close();
		}
		return false;
	}
}
